package com.huawei123.hasee.cniao5store.mvp.interactor;

import com.huawei123.hasee.cniao5store.api.IGetDataDelegate;
import com.huawei123.hasee.cniao5store.api.RecommendApi;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.bean.RecommendBean;
import com.huawei123.hasee.cniao5store.utils.JsonParseUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import javax.inject.Inject;

/**
 * 项目名称：Cniao5Store
 * 类描述：推荐页关联交互
 * 创建人：luohao
 * 创建时间：2018/1/28 18:25
 * 修改人：luohao
 * 修改时间：2018/1/28 18:25
 * 修改备注：
 */

public class RecommendInteractor {
    private IGetDataDelegate<RecommendBean> delegate;

    @Inject
    public RecommendInteractor() {
    }

    /**
     * 执行网络请求
     * 从网络获取数据
     *
     * @param activity
     * @param delegate
     */
    public void loadRecommendData(BaseActivity activity, IGetDataDelegate<RecommendBean> delegate) {
        this.delegate = delegate;
        RecommendApi recommendApi = new RecommendApi(listener, activity);
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(recommendApi);
    }

    HttpOnNextListener listener = new HttpOnNextListener<RecommendBean>() {
        @Override
        public void onNext(RecommendBean recommendBean) {
            delegate.getDataSuccess(recommendBean);
        }
        /**
         * 缓存方法
         * @param string
         */
        @Override
        public void onCacheNext(String string) {
            super.onCacheNext(string);
            RecommendBean recommendBean = JsonParseUtils.parseRecommendBean(string);
            delegate.getDataSuccess(recommendBean);
        }
        @Override
        public void onError(Throwable e) {
            super.onError(e);
            delegate.getDataError(e.getMessage());
        }
    };
}
